package com.nulldev.util.manage;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.VariableAPI.util.strings.splitters.Splitters;

public class Version {

	public final int MAJOR_VERSION, MINOR_VERSION, SUB_VERSION;

	public Version(final int MAJOR, final int MINOR) {
		this(MAJOR, MINOR, -1);
	}

	public Version(final int MAJOR, final int MINOR, final int SUB) {
		this.MAJOR_VERSION = MAJOR;
		this.MINOR_VERSION = MINOR;
		this.SUB_VERSION = SUB;
	}

	public Version(final String MAJOR, final String MINOR, final String SUB) {
		this.MAJOR_VERSION = Integer.parseInt(MAJOR);
		this.MINOR_VERSION = Integer.parseInt(MINOR);
		this.SUB_VERSION = Integer.parseInt(SUB);
	}

	public Version(final String VERSION) {
		final String[] vers = Splitters.splitString(VERSION, ".|_");
		this.MAJOR_VERSION = Integer.parseInt(vers[0]);
		this.MINOR_VERSION = Integer.parseInt(vers[1]);
		if (vers.length > 2) {
			this.SUB_VERSION = Integer.parseInt(vers[2]);
		} else
			this.SUB_VERSION = -1;
	}

	@Override
	public String toString() {
		if (this.SUB_VERSION == -1)
			return this.MAJOR_VERSION + "." + this.MINOR_VERSION;
		return this.MAJOR_VERSION + "." + this.MINOR_VERSION + "." + this.SUB_VERSION;
	}

	public boolean is(final int MAJOR, final int MINOR, final int SUB) {
		return this.MAJOR_VERSION == MAJOR && this.MINOR_VERSION == MINOR && this.SUB_VERSION == SUB;
	}

	public boolean is(final Version ver) {
		return this.MAJOR_VERSION == ver.MAJOR_VERSION && this.MINOR_VERSION == ver.MINOR_VERSION && this.SUB_VERSION == ver.SUB_VERSION;
	}

	public boolean is(final String VERSION) {
		final String[] vers = Splitters.splitString(VERSION, ".");
		final int MAJOR = Integer.parseInt(vers[0]);
		final int MINOR = Integer.parseInt(vers[1]);
		final int SUB;
		if (vers.length > 2) {
			SUB = Integer.parseInt(vers[2]);
		} else
			SUB = -1;
		return this.MAJOR_VERSION == MAJOR && this.MINOR_VERSION == MINOR && this.SUB_VERSION == SUB;
	}
//	
//	public boolean newer(final int MAJOR, final int MINOR, final int SUB) {
//		return MAJOR > this.MAJOR_VERSION  && MINOR > this.MINOR_VERSION && SUB > this.SUB_VERSION;
//	}
//	
//	public boolean newer(final Version ver) {
//		return ver.MAJOR_VERSION > this.MAJOR_VERSION && ver.MINOR_VERSION > this.MINOR_VERSION && ver.SUB_VERSION > this.SUB_VERSION;
//	}
//	
//	public boolean newer(final String VERSION) {
//		final String[] vers = Splitters.splitString(VERSION, ".");
//		final int MAJOR = Integer.parseInt(vers[0]);
//		final int MINOR = Integer.parseInt(vers[1]);
//		final int SUB;
//		if (vers.length >= 2) {
//			SUB = Integer.parseInt(vers[2]);
//		}else SUB = -1;
//		return MAJOR > this.MAJOR_VERSION && MINOR > this.MINOR_VERSION && SUB >= this.SUB_VERSION;
//	}

	public boolean newerThan(final int MAJOR, final int MINOR, final int SUB) {
		return this.MAJOR_VERSION >= MAJOR && this.MINOR_VERSION >= MINOR && this.SUB_VERSION >= SUB;
	}

	public boolean newerThan(final Version ver) {
		return ver.MAJOR_VERSION >= this.MAJOR_VERSION && ver.MINOR_VERSION >= this.MINOR_VERSION && ver.SUB_VERSION >= this.SUB_VERSION;
	}

	public boolean newerThan(final String VERSION) {
		final String[] vers = Splitters.splitString(VERSION, ".");
		final int MAJOR = Integer.parseInt(vers[0]);
		final int MINOR = Integer.parseInt(vers[1]);
		final int SUB;
		if (vers.length > 2) {
			SUB = Integer.parseInt(vers[2]);
		} else
			SUB = -1;
		return MAJOR >= this.MAJOR_VERSION && MINOR >= this.MINOR_VERSION && SUB >= this.SUB_VERSION;
	}

	public boolean olderThan(final int MAJOR, final int MINOR, final int SUB) {
		return MAJOR <= this.MAJOR_VERSION && MINOR <= this.MINOR_VERSION && SUB <= this.SUB_VERSION;
	}

	public boolean olderThan(final Version ver) {
		return ver.MAJOR_VERSION <= this.MAJOR_VERSION && ver.MINOR_VERSION <= this.MINOR_VERSION && ver.SUB_VERSION <= this.SUB_VERSION;
	}

	public boolean olderThan(final String VERSION) {
		final String[] vers = Splitters.splitString(VERSION, ".");
		final int MAJOR = Integer.parseInt(vers[0]);
		final int MINOR = Integer.parseInt(vers[1]);
		final int SUB;
		if (vers.length > 2) {
			SUB = Integer.parseInt(vers[2]);
		} else
			SUB = -1;
		return MAJOR <= this.MAJOR_VERSION && MINOR <= this.MINOR_VERSION && SUB <= this.SUB_VERSION;
	}

	public static void main(String[] args) {
		final Version v = new Version("1.5.4");
		JVM.println("is(1.5.4): " + v.is("1.5.4"));
		JVM.println("older(1.4.5): " + v.olderThan(1, 4, 5));
		JVM.println("olderThan(1.4.14): " + v.olderThan(1, 5, 14));
		JVM.println("newer(1.5.5): " + v.newerThan(1, 5, 5));
		JVM.println("newerThan(1.5.14): " + v.newerThan("1.5.14"));
	}

	public String asVersionString() {
		if (this.SUB_VERSION == -1) {
			return this.MAJOR_VERSION + "." + this.MINOR_VERSION;
		} else {
			return this.MAJOR_VERSION + "." + this.MINOR_VERSION + "." + this.SUB_VERSION;
		}
	}
}
